package com.neiquan.backstage.weixin.web.action;

import java.util.Arrays;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neiquan.backstage.common.util.CommonResp;
import com.neiquan.backstage.common.util.Sessions;
import com.neiquan.backstage.platform_alliance_member.PlatformAllianceMemberService;
import com.neiquan.backstage.pub.bsc.dao.po.AdminUser;
import com.neiquan.backstage.pub.bsc.dao.po.PlatformAllianceMember;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.bean.WxMenu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpServiceImpl;

@Controller
@RequestMapping(value = "weixin", produces = { "text/json;charset=UTF-8" })
public class WeixinAction {
	@Autowired
	private PlatformAllianceMemberService allianceMemberService;

	@ResponseBody
	@RequestMapping("updateMenu")
	public String updateMenu(HttpSession session) {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		PlatformAllianceMember allianceMember = allianceMemberService.queryById(loginedUser.getMemberId());
		WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage = new WxMpInMemoryConfigStorage();
		wxMpInMemoryConfigStorage.setAppId(allianceMember.getWeixinAppId());
		wxMpInMemoryConfigStorage.setSecret(allianceMember.getWeixinAppSecret());
		wxMpInMemoryConfigStorage.setToken(allianceMember.getWeixinToken());
		wxMpInMemoryConfigStorage.setAesKey(allianceMember.getWeixinEncodingAesKey());
		WxMpService wxMpService = new WxMpServiceImpl();
		wxMpService.setWxMpConfigStorage(wxMpInMemoryConfigStorage);

		WxMenu wxMenu = buildMenu( //
				buildButton(WxConsts.BUTTON_VIEW, "会员服务", null, allianceMember.getWeixinH5UrlMembershipService()), //
				buildButton(WxConsts.BUTTON_CLICK, "干货分享", null, null, //
						buildButton(WxConsts.BUTTON_VIEW, "便利服务", null, allianceMember.getWeixinH5UrlConvenienceService()), //
						buildButton(WxConsts.BUTTON_VIEW, "活动", null, allianceMember.getWeixinH5UrlEvent()), //
						buildButton(WxConsts.BUTTON_VIEW, "干货分享", null, allianceMember.getWeixinH5UrlArticle())), //
				buildButton(WxConsts.BUTTON_CLICK, "创业助手", null, null, //
						buildButton(WxConsts.BUTTON_VIEW, "创业服务", null, allianceMember.getWeixinH5UrlEntrepreneurService()), //
						buildButton(WxConsts.BUTTON_VIEW, "合作伙伴", null, allianceMember.getWeixinH5UrlEntrepreneur()), //
						buildButton(WxConsts.BUTTON_VIEW, "入孵企业", null, allianceMember.getWeixinH5UrlPartner())) //
		);
		try {
			wxMpService.menuCreate(wxMenu);
		} catch (WxErrorException e) {
			return CommonResp.buildErrorResp(e).toJsonString();
		}
		return CommonResp.buildSuccessResp().toJsonString();
	}

	private static WxMenu buildMenu(WxMenuButton... buttons) {
		WxMenu wxMenu = new WxMenu();
		wxMenu.setButtons(Arrays.asList(buttons));
		return wxMenu;
	}

	private static WxMenuButton buildButton(String type, String name, String key, String url, WxMenuButton... subButtons) {
		WxMenuButton button = new WxMenuButton();
		button.setType(type);
		button.setName(name);
		button.setKey(key);
		button.setUrl(url);
		button.setSubButtons(Arrays.asList(subButtons));
		return button;
	}
}
